﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Userslib
{
    /*
     * Class which is able to contact the webservice for the information about race ID's and race names.
     */
    public abstract class Races
    {
        private static Dictionary<int, String> RacesList;

        private static void getRaces()
        {

            localhost.GetData WebServ = new localhost.GetData();
           
            Dictionary<int, String> Dict = new Dictionary<int, String>();
            
            foreach (localhost.Races Race in WebServ.Get_Races())
            {
                Dict.Add(Race.RaceID, Race.RaceName);
            }

            RacesList = Dict;
        }

        public static bool isValidRace(String Race)
        {
            try
            {
                if (RacesList == null)
                    getRaces();
                Race = char.ToUpper(Race[0]) + Race.Substring(1).ToLower(); ;
                var Valid = (RacesList.ContainsValue(Race)) ? true : false;
                if (!Valid)
                    throw new RaceException("Invalid race specified!");
                return Valid;
            }

            catch (Exception exc)
            {
                if (exc.GetType().Equals(typeof(RaceException)))
                    throw exc;
                else
                    return false;
            }
        }

        public static String getRace(int Race)
        {
            try
            {
                if (RacesList == null)
                    getRaces();

                return RacesList.Where(x => x.Key == Race).FirstOrDefault().Value;
            }
            catch (Exception exc)
            {
                return String.Empty;
            }
        }
    }
}
